package com.gouaixin.bee.beeframework2.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gouaixin.bee.beeframework2.R;
import com.gouaixin.bee.beeframework2.adapter.FangkeAdapter;
import com.gouaixin.bee.beeframework2.bean.FANGKE;
import com.gouaixin.bee.beeframework2.bean.USER;
import com.gouaixin.bee.beeframework2.bean.VipType;
import com.gouaixin.bee.beeframework2.dao.FangkeDao;
import com.gouaixin.bee.beeframework2.model.DialogModel;
import com.gouaixin.bee.beeframework2.model.MemberModel;
import com.gouaixin.bee.beeframework2.tool.PreferencesUtils;
import com.gouaixin.bee.beeframework2.tool.TimeUtil;
import com.gouaixin.bee.beeframework2.tool.mLog;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2015/3/9.
 */

//最近访客
public class B3_fangkeActivity extends FragmentActivity {
    Context context = B3_fangkeActivity.this;

    //顶部
    ImageView back;//返回
    TextView title;//标题

    //底部
    RelativeLayout foot;


    //内容区域
    GridView gridView;
    FangkeAdapter adapter = new FangkeAdapter(context);
    List<FANGKE> datas = new ArrayList<FANGKE>();

    TextView size;//访客人数
    String mSize = "";
    TextView name;//当前用户昵称
    String mName = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.b3_fangke);

        initView();

        setListener();

        setData();

    }

    boolean isVip = false;//是否是VIP

    private void initView() {
        back = (ImageView) findViewById(R.id.back);
        foot = (RelativeLayout) findViewById(R.id.footer);
        name = (TextView) findViewById(R.id.name);
        size = (TextView) findViewById(R.id.size);

        gridView = (GridView) findViewById(R.id.gridview);
        gridView.setAdapter(adapter);
        gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));

    }

    private void setListener() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        foot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Toast.makeText(context, "升级会员" + "功能正在开发中", Toast.LENGTH_SHORT).show();
                Intent k1 = new Intent(context, B14_vipTequanActivity.class);
                startActivity(k1);
            }
        });

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
//                AbToastUtil.showToast(context,"您选择的是："+position);
                final String jid=datas.get(position).getJid();

                //1.首先判断是不是第一个.是则点击进入详情
                if(position==0){
                    Intent it = new Intent().setClass(context, B6_ziliaoActivity.class);
                    it.putExtra("HerId",jid);
                    startActivity(it);
                    return;
                }

                //2.判断是否是VIP用户
                MemberModel.setGetUserLister(new MemberModel.GetUserLister() {
                    @Override
                    public void onSuccess(USER user) {
                        long vipEndTime = user.getVipEndTime();
                        long currentTime = TimeUtil.getCurrentTime();
                        if (currentTime <= vipEndTime) {
                            isVip = true;
                            mLog.e("我是VIP");
                            mLog.e("进入对象详情界面");
                            Intent it = new Intent().setClass(context, B6_ziliaoActivity.class);
                            it.putExtra("HerId", jid);
                            startActivity(it);

                        } else {
                            isVip = false;
                            MemberModel.vipSuccess(context, VipType.Type_0);
                            mLog.e("我不是VIP");
                            mLog.e("弹出VIP服务对话框");

                            DialogModel.goVIP(context, "你还没有开通VIP，无法查看资料！");
                        }

                    }

                    @Override
                    public void onFailure(int i, String s) {

                    }
                });
                MemberModel.getUSER(context);


            }
        });

    }


    private void setData() {
        mName = PreferencesUtils.getString(context, "cname");
        name.setText("Hi," + mName);

        datas = FangkeDao.query(context);
        if (datas != null) {
            adapter.setData(datas);
            adapter.notifyDataSetChanged();
            int number = datas.size();
            size.setText(number + "");
        }

    }


}
